package createnewobject;

import java.io.Serializable;
import java.lang.reflect.Constructor;

class Person implements Serializable,Cloneable {

    private String name;
    private String gender;
    private String birthday;
    //必须提供默认的无参的构造方法
    public Person() {
    }

    public Person(String name, String gender, String birthday) {
        this.name = name;
        this.gender = gender;
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", birthday='" + birthday + '\'' +
                '}';
    }

    //必须重写clone方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class CreateNewObjectByReflectionDemo {

    public static void main(String[] args) throws Exception {
        //获取类对象
        Class clazz = Class.forName("createnewobject.Person");
        Person p = (Person) clazz.newInstance();//通过反射创建对象。
        System.out.println(p);

        //通过反射获取带三个参数的构造方法对象。
        Constructor constructor  = clazz.getDeclaredConstructor(String.class,String.class,String.class);

        Person p2 = (Person) constructor.newInstance("张三","男","1999-10-10");
        System.out.println(p2);
    }
}
